package cn.kgc.demo01.service.impl;

import cn.kgc.demo01.mapper.PersonMapper;
import cn.kgc.demo01.pojo.Person;
import cn.kgc.demo01.pojo.PersonExample;
import cn.kgc.demo01.service.PersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService{
    @Resource
    PersonMapper personMapper;

    @Override
    public PageInfo<Person> select(Integer pageNum, Integer pageSize, String department) {
        PageHelper.startPage(pageNum,pageSize);
        List<Person> list;
        if(department!=null&&!department.equals("")){
            PersonExample example=new PersonExample();
            PersonExample.Criteria criteria=example.createCriteria();
            criteria.andDepartmentLike(department);
            list=personMapper.selectByExample(example);
        }else {
            list=personMapper.selectByExample(null);
        }
        PageInfo pageInfo=new PageInfo<>(list);
        return pageInfo ;
    }

    @Override
    public Integer add(Person person) {
        int i=personMapper.insert(person);
        return i;
    }

    @Override
    public Person selectById(Integer id) {
        Person person=personMapper.selectByPrimaryKey(id);
        return person;
    }

    @Override
    public Integer update(Person person) {
        int i=personMapper.updateByPrimaryKey(person);
        return i;
    }

    @Override
    public Integer delete(Integer id) {
        int i=personMapper.deleteByPrimaryKey(id);
        return i;
    }
}
